<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>eventloop</title>
  </head>
  <style>
    .box {
      width: 100px;
      height: 30px;
      text-align: center;
      line-height: 30px;
      cursor: pointer;
      border: 1px solid salmon;
    }
    .box:hover {
      background-color: sandybrown;
      opacity: 0.8;
    }
    #view {
      width: 500px;
      margin: 0 20px;
      height: 600px;
      overflow: hidden;
      overflow-y: scroll;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    }
  </style>
  <body>
    <div style="display: flex">
      <div>
        <div id="btn" class="box">阻塞render</div>
        <p></p>
        <div id="btn2" class="box">非阻塞render</div>
      </div>
      <div id="view"></div>
    </div>
  </body>
</html>

<script>
  function wait(time, cb) {
    const now = Date.now();
    while (true) {
      if (Date.now() - now > time) break;
    }
    cb && cb();
  }

  function sleep(time, cb) {
    return new Promise((resolve) => {
      setTimeout(() => {
        cb && cb();
        resolve();
      }, time);
    });
  }

  (function () {
    // console.log(1);
    // // 阻塞
    // wait(1000);
    // console.log(2);
    // // 非阻塞
    // setTimeout(() => {
    //   console.log(3);
    // }, 1000);
    // console.log("end");
  })();

  (function () {
    const btn = document.querySelector("#btn");
    const view = document.querySelector("#view");
    view.innerHTML = "";
    btn.addEventListener("click", function () {
      // wait(2000, function () {
      //   console.log("2s");
      // });
      // sleep(2000, function () {
      //   console.log("2s");
      // });
      // Promise.resolve().then(() => {
      //   console.log("==");
      // });
      // wait(2000, function () {
      //   console.log("2s");
      // });

      setTimeout(() => {
        console.log(1);
      }, 1000);
      setTimeout(() => {
        console.log(2);
      }, 5000);

      // 优先于settimeout
      requestAnimationFrame(() => {
        console.log(-1);
      });
      // 阻塞渲染
      for (let y = 5000; y > 0; y--) {
        const fragment = new DocumentFragment();
        let last = null;
        for (let i = 50; i > 0; i--) {
          const div = document.createElement("div");
          div.innerHTML = y + "-" + i;
          fragment.appendChild(div);
          last = div;
        }
        view.appendChild(fragment);
        prev = last;
      }
      console.log(0);
    });
  })();

  (function () {
    const btn = document.querySelector("#btn2");
    const view = document.querySelector("#view");
    view.innerHTML = "";

    btn.addEventListener("click", function () {
      setTimeout(() => {
        console.log(1);
      }, 1000);
      setTimeout(() => {
        console.log(2);
      }, 5000);

      // 优先于settimeout
      requestAnimationFrame(() => {
        console.log(-1);
      });
      for (let y = 5000; y > 0; y--) {
        setTimeout(() => {
          const fragment = new DocumentFragment();
          let last = null;
          for (let i = 50; i > 0; i--) {
            const div = document.createElement("div");
            div.innerHTML = y + "-" + i;
            fragment.appendChild(div);
            last = div;
          }
          view.appendChild(fragment);
          prev = last;
        });
      }
    });
  })();
</script>
